/*
    Text File Merger
    Version 1.0
    Date 20:05 20/07/2019
    By DreamVB
*/

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char**argv[])
{
    int x = 2;
    FILE *fout = NULL;
    FILE *fIn = NULL;

    //Check for parameters
    if(argc <= 1){
        printf("Not enough parameters to complete task.\n");
        return 0;
    }

    //Get first file this is what we will be writing to.
    fout = fopen(argv[1],"wb");

    //Check if output file was opened.
    if(fout == NULL){
        printf("Failed to write to output filename %s\n",argv[1]);
        return 0;
    }

    //Get input files.
    while(x < argc){
        //Pointer to input filename
        fIn = fopen(argv[x],"rb");
        //Check input filename was opened.
        if(!fIn){
            printf("Cannot open input filename %s\n",argv[x]);
            break;
        }
        //While not end of input file.
        while(!feof(fIn)){
            //Read single char
            char c = fgetc(fIn);
            //Again check we are not at the end of the file.
            if(!feof(fIn)){
                //Write char to output file.
                fputc(c,fout);
            }
        }
        //Close input file.
        fclose(fIn);
        //INC x counter
        x++;
    }
    //Close output file.
    fclose(fout);
    //Return back to the system.
    return 0;
}
